/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import cn.binarywang.wx.miniapp.bean.urllink.request.QueryUrlLinkRequest;
import cn.binarywang.wx.miniapp.bean.urllink.response.QueryUrlLinkResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import dm.jdbc.util.StringUtil;
import io.renren.common.utils.Result;
import io.renren.controller.im.TencentCloudImUtil;
import io.renren.dto.*;
import io.renren.dto.queryDto.ParByKeyQuery;
import io.renren.dto.queryDto.StoreQuery;
import io.renren.dto.queryDto.VinQuery;
import io.renren.dto.repon.UcmlOrganizeRepon;
import io.renren.entity.ChatgroupEntity;
import io.renren.entity.ChatmemberEntity;
import io.renren.entity.ImgroupallbillEntity;
import io.renren.entity.TradermEntity;
import io.renren.factory.FeginClientFactory;
import io.renren.service.*;
import io.renren.utils.WechatMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Index")
@Api(tags = "Index接口")
@AllArgsConstructor
public class ApiIndexNewController {

    @Autowired
    private TencentCloudImUtil tencentCloudImUtil;

    @Autowired
    private ChatgroupService chatgroupService;


    @PostMapping("addChatGroupMember")
    @ApiOperation("群创建成功后，加入成员")
    public Result<String> addChatGroupMember(@RequestBody AddChatGroupMemberDTO addChatGroupMemberDTO) {
        String result = tencentCloudImUtil.addGroupMember(addChatGroupMemberDTO.getGroupId(), addChatGroupMemberDTO.getMemberList());
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("OK".equals(jsonObject.getString("ActionStatus"))) {
            return new Result().ok("加入成功");
        }
        return new Result().error(jsonObject.getString("ErrorInfo"));
    }


    @PostMapping("createChatRoom")
    @ApiOperation("创建群")
    public Result<String> createChatRoom(@RequestBody CreateChatRoomDTO createChatRoomDTO) {
        if (StringUtil.isEmpty(createChatRoomDTO.getOwnerAccount())) {
            createChatRoomDTO.setOwnerAccount("00000000-0000-0000-0000-000000000001");
        }
        ChatgroupEntity chatgroupEntity = chatgroupService.findOneByIMGroupID(createChatRoomDTO.getGroupId());
        if (chatgroupEntity != null) {
            return new Result().ok(chatgroupEntity.getImgroupid());
        }

        String result = tencentCloudImUtil.createGroup(createChatRoomDTO.getVinCode(), createChatRoomDTO.getName(), createChatRoomDTO.getOwnerAccount());
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("OK".equals(jsonObject.getString("ActionStatus"))) {
            Map<String, Object> res = new HashMap<>();
            res.put("GroupId", jsonObject.getString("GroupId"));
            return new Result().ok(res);
        }
        return new Result().error(jsonObject.getString("ErrorInfo"));
    }


    @PostMapping("sendGroupMsg")
    @ApiOperation("在群组中发送普通消息")
    public Result<String> sendGroupMsg(@RequestBody SendGroupMsgDTO dto) {
        String result = tencentCloudImUtil.sendGroupMsg(dto.getGroupId(), dto.getContent(), dto.getFromAccount());
        JSONObject jsonObject = JSONObject.parseObject(result);
        return new Result().ok(jsonObject);
    }

    @Autowired
    private ChatmemberService chatmemberService;

    @PostMapping("delGroupMember")
    @ApiOperation("删除群成员（除群主外的所有人）")
    public Result<String> delGroupMember(@RequestBody DelGroupMemberDTO dto) {
        List<ChatmemberEntity> chatmemberEntities = chatmemberService.getListByGroupId(dto.getGroupId());
        if (chatmemberEntities.isEmpty()) {
            return new Result().error("群成员已删除");
        }
        String accounts = chatmemberEntities.stream().map(ChatmemberEntity::getUserid).reduce((a, b) -> a + "," + b).get();
        String result = tencentCloudImUtil.deleteGroupMember(dto.getGroupId(), accounts);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("OK".equals(jsonObject.getString("ActionStatus"))) {
            chatmemberService.deleteByGroupId(dto.getGroupId());
            return new Result().ok("删除群成员成功");
        }
        return new Result().error(jsonObject.getString("ErrorInfo"));
    }

    @PostMapping("dismissGroup")
    @ApiOperation("解散群聊")
    public Result<String> dismissGroup(@RequestBody DelGroupMemberDTO dto) {

        String result = tencentCloudImUtil.disbandGroup(dto.getGroupId());
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("OK".equals(jsonObject.getString("ActionStatus"))) {
            chatgroupService.deleteByGroupId(dto.getGroupId());
            return new Result().ok("");
        }
        return new Result().error(jsonObject.getString("ErrorInfo"));
    }


    @PostMapping("createUser")
    @ApiOperation("人员创建接口")
    public Result<String> createUser(@RequestBody CreateUserDTO dto) {
        if (StringUtil.isEmpty(dto.getUserId())) {
            return new Result().error("参数错误");
        }
        String result = tencentCloudImUtil.accountImport(dto.getUserId(), dto.getNick(), dto.getFaceUrl());
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("OK".equals(jsonObject.getString("ActionStatus"))) {
            return new Result().ok(jsonObject);
        }
        return new Result().error(jsonObject.getString("ErrorInfo"));
    }


    @PostMapping("sendChatGroupMsg")
    @ApiOperation("发送群通知")
    public Result<String> sendChatGroupMsg(@RequestBody SendChatGroupMsgDTO dto) {
        String result = tencentCloudImUtil.sendGroupSystemNotification(dto.getGroupId(), dto.getContent());
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("OK".equals(jsonObject.getString("ActionStatus"))) {
            return new Result().ok(jsonObject);
        }
        return new Result().error(jsonObject.getString("ErrorInfo"));
    }
}